package com.funquer.cms.controller;

import com.funquer.admin.pageModel.DataGrid;
import com.funquer.admin.pageModel.Json;
import com.funquer.cms.model.ChanneltypeEntity;
import com.funquer.cms.pageModel.ChanneltypePage;
import com.funquer.cms.service.ChanneltypeServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-17
 * Time: 上午10:39
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/channeltypeController")
public class ChanneltypeController {


    private ChanneltypeServiceI channeltypeService;

    public ChanneltypeServiceI getChanneltypeService() {
        return channeltypeService;
    }

    @Autowired
    public void setChanneltypeService(ChanneltypeServiceI channeltypeService) {
        this.channeltypeService = channeltypeService;
    }

    @RequestMapping("/index")
    public String index(){
        return "cms/channeltype/index";
    }


    @RequestMapping("/datagrid")
    @ResponseBody
    public DataGrid datagrid(ChanneltypePage channeltypePage) {
        return channeltypeService.datagrid(channeltypePage);
    }


    @RequestMapping("/toAdd")
    public String toAdd(){
        return "cms/channeltype/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Json add(ChanneltypeEntity channeltypeEntity) {
        Json j = new Json();
        try {
            ChanneltypeEntity r = channeltypeService.save(channeltypeEntity);
            j.setSuccess(true);
            j.setMsg("添加成功！");
            j.setObj(r);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }


    @RequestMapping("/edit")
    @ResponseBody
    public Json edit(ChanneltypeEntity channeltypeEntity) {
        Json j = new Json();
        try {
            ChanneltypeEntity r = channeltypeService.edit(channeltypeEntity);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
            j.setObj(r);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }


    @RequestMapping("/toEdit")
    public String toEdit(String id, HttpServletRequest request){
        ChanneltypeEntity entity= channeltypeService.getOne(id);
        request.setAttribute("entity",entity);
        return "cms/channeltype/edit";
    }


    @RequestMapping("/remove")
    @ResponseBody
    public Json remove(ChanneltypePage channeltypePage){
        channeltypeService.remove(channeltypePage.getIds());
        Json j = new Json();
        j.setSuccess(true);
        j.setMsg("删除成功！");
        return j;
    }


}
